//
//  YZAddFriendVC.m
//  VET
//
//  Created by lixiaoyi on 2018/7/13.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZAddFriendVC.h"
#import "ChatHeader.h"
#import "YZSearchFriendVC.h"
#import "YZFriendModel.h"
#import "YZFriendAddCell.h"
#import "YZApplyFriendVC.h"

static NSString *flagCell = @"YZFriendAddCell";

@interface YZAddFriendVC ()<UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating>

@property (nonatomic,strong) UITableView *addFriendTable;
@property (nonatomic,strong) NSMutableArray *addFriendData;
@property (nonatomic,strong) NSMutableArray *searchResultData;
@property (nonatomic,strong) UISearchController *searchController;

@end

@implementation YZAddFriendVC

#pragma mark - lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"添加朋友";
    [self createViews];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // 打开导航
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    // 隐藏导航
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

#pragma mark - ui
- (void)createViews{
//    YZSearchFriendVC *searchFriendVC = [YZSearchFriendVC new];
    UISearchController *tempSearchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    [tempSearchController.searchBar sizeToFit];
    tempSearchController.searchResultsUpdater = self;
    
    self.addFriendTable.tableHeaderView = tempSearchController.searchBar;
    _searchController = tempSearchController;
    
    [tempSearchController.searchBar setBarTintColor:XZRGB(0xf4f1f1)];
    [tempSearchController.searchBar.layer setBorderWidth:0.5f];
    [tempSearchController.searchBar.layer setBorderColor:XZRGB(0xf4f1f1).CGColor];
    
    tempSearchController.dimsBackgroundDuringPresentation = NO;
    self.definesPresentationContext = YES;
    tempSearchController.hidesNavigationBarDuringPresentation = YES;
    tempSearchController.searchBar.placeholder = @"用户名称或手机号码";
    
    [self.view addSubview:self.addFriendTable];
}

#pragma mark - lazy load
- (UITableView *)addFriendTable {
    if (!_addFriendTable) {
        CGRect tableViewFrame = CGRectMake(0, 0, kWidth, kHeight);
        _addFriendTable = [[UITableView alloc] initWithFrame:tableViewFrame style:UITableViewStyleGrouped];
        _addFriendTable.delegate = self;
        _addFriendTable.dataSource = self;
        [_addFriendTable registerClass:[YZFriendAddCell class] forCellReuseIdentifier:flagCell];
    }
    
    return _addFriendTable;
}

- (NSMutableArray *)addFriendData{
    if (!_addFriendData) {
        _addFriendData = [NSMutableArray array];
        
        for (int i = 0; i< 10; i++) {
            YZFriendModel *model = [YZFriendModel new];
            model.iconName = @"";
            int nameRandom = arc4random() % 3;
            if (nameRandom == 0) {
                model.name = @"李思";
            } else if (nameRandom == 1) {
                model.name = @"江大力";
            } else if (nameRandom == 2) {
                model.name = @"高田男";
            }
            model.info = @"兽医教授";
            model.targetId = @"";
            model.friendState = NewFriendStateAdd;
            [_addFriendData addObject:model];
        }
    }
    
    return _addFriendData;
}

- (NSMutableArray *)searchResultData{
    if (!_searchResultData) {
        _searchResultData = [NSMutableArray array];
    }
    
    return _searchResultData;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.searchController.active) {
        return [self.searchResultData count];
    } else {
        return [self.addFriendData count];
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YZFriendAddCell *cell = [tableView dequeueReusableCellWithIdentifier:flagCell];
    if (!cell) {
        cell = [[YZFriendAddCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:flagCell];
    }

    if (self.searchController.active) {
        YZFriendModel *model = [self.searchResultData objectAtIndex:indexPath.row];
        cell.model = model;
    } else {
        YZFriendModel *model = [self.addFriendData objectAtIndex:indexPath.row];
        cell.model = model;
    }
    
    cell.addFriendBlock = ^{
        // 验证申请
        YZApplyFriendVC *applyVC = [[YZApplyFriendVC alloc] init];
        [self.navigationController pushViewController:applyVC animated:YES];
    };
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 54;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击了------->%ld",indexPath.row);
}

#pragma mark -UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    
    NSString *str = searchController.searchBar.text;
    if (self.searchResultData.count > 0) {
        [self.searchResultData removeAllObjects];
    }
    
    for (YZFriendModel *friendModel in self.addFriendData) {
        if ([friendModel.name.lowercaseString rangeOfString:str.lowercaseString].location != NSNotFound) {
            [self.searchResultData addObject:friendModel];
        }
    }
    
    [self.addFriendTable reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
